package com.culturaltourism.core.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: SysInterceptor
 * @Description: 请求拦截
 * @Author MaShuai
 * @Date 2019/5/2 17:42
 * @Version V1.0
 */
public class SysInterceptor implements HandlerInterceptor {

    private static Logger log = LoggerFactory.getLogger(SysInterceptor.class);

    //private WxmsApplicationRunner wxmsApplicationRunner;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("开始进入请求地址拦截 !");
        HttpSession session = request.getSession();
        if(StringUtils.isEmpty(session.getAttribute("user"))){
            log.info("session 是空的，请重新登录 !");
            response.sendRedirect("/login");
            return false;
        } else {
            log.info("session:" + session.getAttribute("user"));
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        log.info("处理请求完成后视图渲染之前的处理操作 !");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        log.info("视图渲染之后的操作 !");
    }
}
